home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / IPC / Open2.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  2.4 KB  |  78 lines

  1. package IPC::Open2;
  2.  
  3. use strict;
  4. use vars qw($VERSION @ISA @EXPORT);
  5.  
  6. require 5.000;
  7. require Exporter;
  8.  
  9. $VERSION    = 1.01;
  10. @ISA        = qw(Exporter);
  11. @EXPORT        = qw(open2);
  12.  
  13. =head1 NAME
  14.  
  15. IPC::Open2, open2 - open a process for both reading and writing
  16.  
  17. =head1 SYNOPSIS
  18.  
  19.     use IPC::Open2;
  20.     $pid = open2(\*RDR, \*WTR, 'some cmd and args');
  21.     $pid = open2(\*RDR, \*WTR, 'some', 'cmd', 'and', 'args');
  22.  
  23. =head1 DESCRIPTION
  24.  
  25. The open2() function spawns the given $cmd and connects $rdr for
  26. reading and $wtr for writing.  It's what you think should work 
  27. when you try
  28.  
  29.     open(HANDLE, "|cmd args|");
  30.  
  31. The write filehandle will have autoflush turned on.
  32.  
  33. If $rdr is a string (that is, a bareword filehandle rather than a glob
  34. or a reference) and it begins with ">&", then the child will send output
  35. directly to that file handle.  If $wtr is a string that begins with
  36. "<&", then WTR will be closed in the parent, and the child will read
  37. from it directly.  In both cases, there will be a dup(2) instead of a
  38. pipe(2) made.
  39.  
  40. open2() returns the process ID of the child process.  It doesn't return on
  41. failure: it just raises an exception matching C</^open2:/>.
  42.  
  43. =head1 WARNING 
  44.  
  45. It will not create these file handles for you.  You have to do this yourself.
  46. So don't pass it empty variables expecting them to get filled in for you.
  47.  
  48. Additionally, this is very dangerous as you may block forever.
  49. It assumes it's going to talk to something like B<bc>, both writing to
  50. it and reading from it.  This is presumably safe because you "know"
  51. that commands like B<bc> will read a line at a time and output a line at
  52. a time.  Programs like B<sort> that read their entire input stream first,
  53. however, are quite apt to cause deadlock.  
  54.  
  55. The big problem with this approach is that if you don't have control 
  56. over source code being run in the child process, you can't control
  57. what it does with pipe buffering.  Thus you can't just open a pipe to
  58. C<cat -v> and continually read and write a line from it.
  59.  
  60. =head1 SEE ALSO
  61.  
  62. See L<IPC::Open3> for an alternative that handles STDERR as well.  This
  63. function is really just a wrapper around open3().
  64.  
  65. =cut
  66.  
  67.  
  68. require IPC::Open3;
  69.  
  70. sub open2 {
  71.     my ($read, $write, @cmd) = @_;
  72.     local $Carp::CarpLevel = $Carp::CarpLevel + 1;
  73.     return IPC::Open3::_open3('open2', scalar caller,
  74.                 $write, $read, '>&STDERR', @cmd);
  75. }
  76.  
  77. 1
  78.